
public abstract class MannedStructure extends Structure {
	

	protected int workers, unassignedWorkers, harvestRT;
	protected StructLinkedList list;
	
	public MannedStructure(int x, int y) {
		super(x, y);
		workers = 0;
		unassignedWorkers = 0;
		harvestRT=20;
		
	}
	
	// ======== GETTERS ================
	public String toString(){return this.getTypeName()+": "+this.getID()+" = "+health+" - Energy: "+energy+" Metal: "+metal+" Food: "+nutrient+" UWorkers: "+unassignedWorkers;}
	
	// ======== SETTERS ================
	public void addWorkers(int x){
		workers += x;
		unassignedWorkers+=x;
	}
	public int removeWorkers(int x){
		if(unassignedWorkers >x){
			workers -= x;
			unassignedWorkers -=x;
		}
		else{
			x = unassignedWorkers;
			workers =-unassignedWorkers;
			unassignedWorkers = 0;
		}
		return x;
	}
	
	public boolean addTile(int x, int y){
		if(map.assign(x,y)){
			map.tiles[x][y].setLabel('Q');
			list.add(map.tiles[x][y]);
			return true;
		}
		return false;
	}

	
	public boolean removeTile(Tile temp){return list.remove(temp);}
	public boolean removeTile(int x, int y){return removeTile(new Tile(x,y));}
	

	// ========= DO STUFF ==============
	public int assign(int num, int x, int y){return assign(num, map.tiles[x][y]);}
	public int assign(int num, Tile tile){
	
		if(num > unassignedWorkers){
			num = unassignedWorkers;
			if(!list.assign(num, tile))
				return -1;
			
			workers =- num;
			unassignedWorkers = 0;
		}	
		else{
			unassignedWorkers -= num;
			workers -=num;
			
			if(!list.assign(num, tile))
				return -1;	
		}
		return num;
	}
	
	public int unassign(int num, Tile tile){
		int temp=list.unassign(num, tile);
		workers += temp;
		unassignedWorkers += temp;
		return temp;
	}
	
	int endTick() {
		int needed=0, total = metalRT + energyRT + nutrientRT;
		
		int temp = list.harvest();
		
		switch(type){
			case 1: nutrient += temp;
			case 2: metal += temp;
			case 3: energy += temp;
			
		}
		
		if(!damage(damage)){
			die();
			return -1;
		}
		damage = 0;

		
		if(metal - metalRT >= 0)
			metal -= metalRT;
		else{
			needed += metalRT - metal;
			metal = 0;
		}
		
		if(energy - energyRT >= 0)
			energy -= energyRT;
		else{
			needed += energyRT - energy;
			energy = 0;
		}
		
		if(nutrient - nutrientRT >= 0)
			nutrient -= nutrientRT;
		else{
			needed += nutrientRT - nutrient;
			nutrient = 0;
		}
		
		if(needed > 0)
			if(damage((float)(needed)/total/2))
				return 0;
			else{
				die();
				return -1;
			}
		
		return temp;
	}
	
	public boolean die() {
		list.die();
		return map.die(x, y, this);
	}
}
